/*
 * Melomano - simple music player for Android
 *
 * Copyright (C) 2011  Andriy Tsykholyas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tsyko.android.melomano.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ListView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tsyko.android.melomano.R;
import tsyko.android.melomano.model.PlayList;

import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class FolderViewActivity extends AbstractMelomanoActivity {

    private static final Logger logger = LoggerFactory.getLogger(FolderViewActivity.class);

    // TODO: persist it
    private File currentDirectory = null;
    private ListView fileListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.folder_view);
        fileListView = (ListView) findViewById(R.id.list_files);
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (currentDirectory == null) {
            currentDirectory = getDefaultDirectory();
        }
        fileListView.setAdapter(new FileListAdapter(this, getFileListItems(currentDirectory)));
    }


    void setCurrentDirectory(File currentDirectory) {
        this.currentDirectory = currentDirectory;
        fileListView.setAdapter(new FileListAdapter(this, getFileListItems(currentDirectory)));
        // TODO: update button "play all" enablement
    }


    public void playAll(View view) {
        final FileListAdapter adapter = (FileListAdapter) fileListView.getAdapter();
        final int count = adapter.getCount();
        List<FileListItem> items = new ArrayList<FileListItem>(count);
        for (int i = 0; i < count; i++) {
            items.add(adapter.getItem(i));
        }
        PlayList playList = createPlayList(items);
        engine.setCurrentPlayList(playList);
        engine.play();

        Intent intent = new Intent(this, PlayingFileActivity.class);
        startActivity(intent);
    }


    // ---------------- utility methods ---------------------


    private static List<FileListItem> getFileListItems(File directory) {

        logger.debug("getFileListItems() dir: {}", directory);

        File[] files = directory.listFiles();
        Arrays.sort(files, new FileComparator());

        List<FileListItem> items = new ArrayList<FileListItem>();
        for (File file : files) {
            items.add(new FileListItem(file));
        }

        logger.debug("getFileListItems() found {} files", items.size());

        return items;
    }


    private static File getDefaultDirectory() {
        return Environment.getExternalStorageDirectory();
    }


    private static PlayList createPlayList(List<FileListItem> items) {
        // TODO
        return null;
    }
}